package models

import (
	"net/smtp"
	"strings"
)

type SmtpUtil struct {
	Host string
	User string
	Password string
}

var (
	SmtpUtilDefault = SmtpUtil{
		Host:"smtp.sina.com:25",
		User:"zhou123wei321@sina.com",
		Password:"375cee47c737b95b",
	}
)

func (this *SmtpUtil) SendToMail(to, subject, body, mailtype string) error {
	hp := strings.Split(this.Host, ":")
	auth := smtp.PlainAuth("", this.User, this.Password, hp[0])
	var content_type string
	if mailtype == "html" {
		content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
	} else {
		content_type = "Content-Type: text/plain" + "; charset=UTF-8"
	}

	msg := []byte("To: " + to + "\r\nFrom: " + this.User + "<" + this.User +
		">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)

	send_to := strings.Split(to, ";")
	err := smtp.SendMail(this.Host, auth, this.User, send_to, msg)
	return err
}
